'''###########################################################################################################################
### File: menu_scroller
### Name: Patrick Delaney, Andrew Oberlin
### Class: CSE 487
### Date:  4/15/2012
### Instructor: Dr.Zmuda
### Assignment: Final Project
### Description: Creates the scrolling list object.
################ 
'''
from direct.gui.DirectGui import *
from pandac.PandaModules import NodePath
from direct.interval.IntervalGlobal import *
from panda3d.core import Vec2,Point3,Vec3,Vec4
from pandac.PandaModules import TextNode
from menu_button import MenuButton
import string
import os

class MenuScroller:
	def __init__(self, parent, name, directory, extension, frameSize, pos, numItemsVisible, 
				 forceHeight, itemFrame_frameSize, itemFrame_pos, command):
		self.parent = parent
		self.name = name
		self.directory = directory
		self.extension = extension
		self.command = command
		
		#Data Members for the Direct Scrolled List Object
		#Information of the Decrement Button
		self.decButton_pos = (0.35, 0, 0.53)
		self.decButton_text = "+"
		
		#Information about the Increment Button
		self.incButton_pos= (0.35, 0, -0.02)
		self.incButton_text = "-"
		
		self.button_borderWidth = (0.005, 0.005)
		self.button_text_scale = 0.1
		
		self.frameSize = frameSize 		#Size of the frame
		self.frameColor = (1,0,0,0) 	#Color of the Frame
		self.pos = pos
		self.items = []				#Items in the list
		self.numItemsVisible = numItemsVisible	#Number of items that show up
		self.forceHeight = forceHeight			#Forces the height of the scroller
		
		self.itemFrame_frameSize = itemFrame_frameSize   #Size of the item frame
		self.itemFrame_pos = itemFrame_pos				 #Position of the itemFrame
		
		self.loadModel()
		self.fillList()
	
	def loadModel(self):
		'''
		upButtonDummy = self.parent.base.aspect2d.attachNewNode('up_button_dummy')
		upDummy = NodePath('up_button_dummy')
		upButtonModel = self.parent.loader.loadModelCopy("models/plane")
		upButtonModel.setTransparency(1)
		upTexture = self.parent.loader.loadTexture("models/gui/game_maker/up_arrow.png")
		upButtonModel.setTexture(upTexture)
		upButtonModel.reparentTo(upDummy)
		upButtonDummy.hide()
		'''
		self.scroller = DirectScrolledList(
			decButton_pos= self.decButton_pos,
			decButton_text = self.decButton_text,
			decButton_text_scale = self.button_text_scale,
			decButton_borderWidth = self.button_borderWidth,
		 
			incButton_pos= self.incButton_pos,
			incButton_text = self.incButton_text,
			incButton_text_scale = self.button_text_scale,
			incButton_borderWidth = self.button_borderWidth,
		 
			frameSize = self.frameSize,
			frameColor = self.frameColor,
			pos = self.pos,
			items = self.items,
			numItemsVisible = self.numItemsVisible,
			forceHeight = self.forceHeight,
			itemFrame_frameSize = self.itemFrame_frameSize,
			itemFrame_pos = self.itemFrame_pos,
			)
		
	'''
	### Name: fillList
	### Author: Andrew Oberlin, Patrick Delaney
	### Parameters: None
	### Description: Fills the list with the characters already saved found in
					 self.directory
	'''
	def fillList(self):
		#TODO: Fill Items Correctly, 
		if(self.directory is not None):
			path = os.path.abspath(self.directory)
			print(os.listdir(path) )
			for item in os.listdir(path):
				print item
				if(string.find(item, self.extension) > -1):  #Change this to proper file ending(s)
					print "Found Correctly"
					# Using your MenuButton class makes things not look right, and it looks difficult to fix. -Patrick
					#self.addItem( MenuButton(self.parent, 'enemy_char_button', 
					#			   None, 
					#			   self.selectedPreMadeChar, Vec3(0.75, 0, 0.35), 
					#			   isMovie = False, text= item,textPos=Vec3(-0.50, 0, -0.025), 
					# 			   scale=Vec3(1.1,1,0.3) ).button)
					self.addItem( DirectButton(text = (item),text_scale=0.1, 
								  borderWidth = (0.01, 0.01), relief=2,
								  command = self.onClick, extraArgs=[item]) )
		'''
		b1 = DirectButton(text = ("Button1", "click!", "roll", "disabled"),
							text_scale=0.1, borderWidth = (0.01, 0.01),
							relief=2)
		b2 = DirectButton(text = ("Button2", "click!", "roll", "disabled"),
									text_scale=0.1, borderWidth = (0.01, 0.01),
									relief=2)
 
		l1 = DirectLabel(text = "Test1", text_scale=0.1)
		l2 = DirectLabel(text = "Test2", text_scale=0.1)
		l3 = DirectLabel(text = "Test3", text_scale=0.1)

		self.addItem(b1)
		self.addItem(b2)
		self.addItem(l1)
		self.addItem(l2)
		self.addItem(l3)
		
		for fruit in ['apple', 'pear', 'banana', 'orange']:
			l = DirectLabel(text = fruit, text_scale=0.1)
			self.addItem(l) 
			'''
	def addItem(self,item):
		self.scroller.addItem(item)
		
	def onClick(self, item):
		self.selected = item
		#print item
		self.command(item, self.directory)
	
	'''
		Our custom load function to load the textures needed for a movie into a
		list. It assumes the the files are named
		"path/name<serial number>.extention"
		
		Params: frames -- The number of frames to load
				name -- The "path/name" part of the filename path
				suffix -- The "extention" part of the path
				padding -- The number of digit the serial number contians:
					e.g. if the serial number is 0001 then padding is 4
	'''
	'''
	def loadTextureMovie(self, frames, name, suffix, padding = 1):
		return [self.parent.loader.loadTexture((name+"_%0"+str(padding)+"d."+suffix) % i) for i in range(frames)]
		

	def mouseOver(self, event):
		if (self.text != None):
			self.textModel.setColor(self.overTextColor)
			
	def mouseOut(self, event):
		if (self.text != None):
			self.textModel.setColor(self.outTextColor)

	def animate(self, task):
		currentFrame = int(task.time * task.fps)
		self.button.setTexture(self.modelTextures[currentFrame % len(self.modelTextures)], 1)
		
		return task.cont
	'''	
	def hide(self):
		self.scroller.hide()
		
	def show(self):
		self.scroller.show()
		
	def removeNode(self):
		self.scroller.removeNode()
		
	def reparentTo(self, dummy):
		self.scroller.reparentTo(dummy)